var IndexedDB = require('./public/js/IndexedDB.js');
var index_db = 0;
var Alldb = {}
var webdb = function (callback) {
    callback = callback || function () {};
    var _IndexedDB = new IndexedDB({
        name: 'mapdb',
        version: "1"
    })
    _IndexedDB.Alldb('drive', function (alldb) {
        //  console.log(alldb)
        Alldb = alldb;
    })
    this.set = function (row, callback) {
        Alldb[row.key] = row;
        _IndexedDB.deleteDataByKey('drive', row.key, function (off, error) {
            _IndexedDB.addData('drive', {
                value: row.value,
                key: row.key
            }, function (row, error) {
                callback()
            })
        })
    };
    this.get = function (key, callback) {
        if (Alldb[key]) {
            callback(Alldb[key])
            return
        }
        _IndexedDB.getDataByKey('drive', key, function (obj) {
            if (obj) {
                callback(obj)
            } else {
                callback(null)
            }
        })
    }
    var self = this;
};
const translate = require('google-translate-api');

var textList = {};
var FillTexts = function () {

}
var init = function () {
    var mapdb = new webdb();
    HTMLCanvasElement.prototype.GetContext = HTMLCanvasElement.prototype.getContext;
    delete HTMLCanvasElement.prototype.getContext;
    FillTexts = function () {
        textList = {};
        index_db++;
    }
    var MsFillText = function (self, text, a, b, c, d, map_key) {
        var __FillText = true;
        var Regexp = /[\u4E00-\u9FFF]+/;
        var fillStyle = self.fillStyle;
        self.strokeStyle = self.fillStyle;
        if (text.match(Regexp)) {
            mapdb.get(text, function (db) {
                if (db == null) {
                    translate(text, {
                        from: 'zh-CN',
                        to: 'ug'
                    }).then(res => {
                        mapdb.set({
                            key: text,
                            value: res.text
                        }, function () {
                            if (self.font.indexOf('NUR_Font') > 0) {

                            } else {
                                self.font = self.font + ',"NUR_Font"'
                            }
                            if (__FillText) {
                                __FillText = false;
                                if (index_db != map_key) {

                                    self.strokeStyle = fillStyle;
                                    self.strokeText(res.text, a, b, c, d);
                                }
                            }
                        })
                    }).catch(err => {
                        if (__FillText) {
                            __FillText = false;
                            self.strokeStyle = fillStyle;
                            self.strokeText(text, a, b, c, d);

                        }
                    });
                } else {
                    if (self.font.indexOf('NUR_Font') > 0) {

                    } else {
                        self.font = self.font + ',"NUR_Font"'
                    }
                    if (__FillText) {
                        __FillText = false;

                        self.strokeStyle = fillStyle;
                        self.strokeText(db.value, a, b, c, d);
                    }
                }
            })
        } else {
            if (__FillText) {
                __FillText = false;
                self.strokeText(text, a, b, c, d);
            }
        }
    }
    HTMLCanvasElement.prototype.getContext = function (name) {
        off = false;
        var C = this.GetContext(name);
        if (C.fillText) {
            var FillText = C.fillText;
            C.FillText = FillText;
            delete C.fillText;
            C.fillText = function (text, a, b, c, d) {
                var self = this;
                new MsFillText(self, text, a, b, c, d, index_db)
                return true;
            }
        }
        return C;
    }
}
init();
var map = new AMap.Map('container', {
    resizeEnable: true, //是否监控地图容器尺寸变化
    mapStyle: "amap://styles/dark"
});
var mapMovestart = function () {

}
var mapMove = function () {

}
var mapMoveend = function () {

    FillTexts();
}
var mapZoomstart = function () {

}
var mapZoom = function () {

}
var mapZoomend = function () {

}
var mapZoomend = function () {
    FillTexts();
}
var showInfoClick = function () {

}
var showInfoDbClick = function () {
    FillTexts();
}
var showInfoMove = function () {

}

var showInfoDragstart = function () {

}
var showInfoDragging = function () {

}
var showInfoDragend = function () {
    FillTexts();
}

map.on('dragstart', showInfoDragstart);
map.on('dragging', showInfoDragging);
map.on('dragend', showInfoDragend);
map.on('click', showInfoClick);
map.on('dblclick', showInfoDbClick);
map.on('mousemove', showInfoMove);
map.off('zoomstart', mapZoomstart);
map.off('zoomchange', mapZoom);
map.off('zoomend', mapZoomend);
map.on('movestart', mapMovestart);
map.on('mapmove', mapMove);
map.on('moveend', mapMoveend);